﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Ndex.Core
{
    [DataContract]
    public class ValidationModel
    {
        [DataMember]
        public List<string> MemberNames { get; set; }

        [DataMember]
        public string Message { get; set; }

        public ValidationModel() 
        {
            MemberNames = new List<string>();
            Message = string.Empty;
        }

        public ValidationModel(string message, string[] memberNames):this()
        {
            Message = message;
            MemberNames.AddRange(memberNames);
        }

        public ValidationModel(string message, string memberName)
            : this()
        {
            Message = message;
            MemberNames.Add(memberName);
        }
    }

    public static class ValidationModelExt
    {
        public static List<ValidationModel> Add(this List<ValidationModel> obj, string message, params string[] memberNames)
        {
            if(!string.IsNullOrWhiteSpace(message))
                obj.Add(new ValidationModel(message, memberNames));
            return obj;
        }

        public static List<ValidationModel> Add(this List<ValidationModel> obj, System.Enum message, params string[] memberNames)
        {
            obj.Add(new ValidationModel(message.DisplayName(), memberNames));
            return obj;
        }

        public static List<ValidationModel> Add(this List<ValidationModel> obj, string message, string memberName, string val)
        {
            if (string.IsNullOrWhiteSpace(val))
                obj.Add(message, memberName);
            return obj;
        }

        public static List<ValidationModel> Add(this List<ValidationModel> obj, string message, string memberName, DateTime? val)
        {
            if (!val.HasValue || val == DateTime.MinValue || val == DateTime.MaxValue)
                obj.Add(message, memberName);
            return obj;
        }

        public static List<ValidationModel> Add(this List<ValidationModel> obj, string message, string memberName, DateTime val)
        {
            if (val == DateTime.MinValue || val == DateTime.MaxValue)
                obj.Add(message, memberName);
            return obj;
        }

        public static List<ValidationModel> Add<T>(this List<ValidationModel> obj, string message, string memberName, T val)
        {
            if (val == null)
                obj.Add(message, memberName);
            return obj;
        }
    }
}
